/**
 * 整站数据搜索
 */
function searchAll() {
    $(".search-box-results").html("");
    let searchval = $("#doc-search").val();
    $('.search-box-results').show()
    if (searchval == "") {
        $(".search-box-results").html("<p><span style='color: red;margin-left: 15px'>请输入想要查询的标题关键字进行搜索哦</span>" +
            " <span style='cursor: pointer;margin-left: 15px' onclick=\"$('.search-box-results').hide(200)\"> 点我 或 按下 ESC 关闭</span></p>");
        return
    }
    var docsTrees = JSON.parse(document.getElementById("docsTree").innerText);
    var index = 0;
    for (const docsTreesKey in docsTrees) {
        let docsTree = docsTrees[docsTreesKey];
        index = nextDocsTree(docsTree, null, index)
    }
    $("#searchResultsCount").html(index);
    // 如果没有搜索到
    if ($(".search-box-results").html() == "") {
        $(".search-box-results").html("<p><span style='color: red;margin-left: 15px'>没有找到相关文档!!!</span>" +
            " <span style='cursor: pointer;margin-left: 15px' onclick=\"$('.search-box-results').hide(200)\"> 点我关闭</span></p>");
    }
}


/**
 * 整站搜索 递归查询数据
 */
function nextDocsTree(docsTree, pnames, index) {
    if (docsTree == null) {
        return index;
    }

    for (const doc of docsTree) {
        let names = doc.name;
        if (pnames != null) {
            names = pnames + " -> " + doc.name;
        }
        index = nextDocsTree(doc.children, names, index);
        index = filterDocText(doc, names, index)
    }
    return index;
}

/**
 * 整站搜索 处理搜索结果数据
 */
function filterDocText(doc, names, index) {
    console.log(names);
    let searchval = $("#doc-search").val().toLowerCase();
    if (doc.showType == 2 && names.toLowerCase().indexOf(searchval) != -1) {
        var pageHierarchy = document.getElementById("pageHierarchy").innerHTML;
        let url = pageHierarchy + encodeURIComponent(doc.typeName) + "/" + encodeURIComponent(doc.name) + ".html";
        // 关键字标红
        names = this.keywordRed(names, searchval, true);
        // 添加html
        let html = null;
        if (index == 0) {
            html = '<div id="index-' + index + '" class="search-box-results-div"><a  href="' + url + '"> ' + "【" + doc.typeName + "】 " + names + ' </a></div>';
        } else {
            html = '<div id="index-' + index + '"><a  href="' + url + '"> ' + "【" + doc.typeName + "】 " + names + ' </a></div>';
        }
        $(".search-box-results").append(html);
        return ++index;
    }
    return index;
}

/**
 * 给搜索关键字标红
 * @param str 内容 (标题/描述/内容等)
 * @param keyword 需要标红的关键字
 * @param isCase  是否匹配大小写（true=是，支持大小写模糊匹配, false= 精准匹配）
 *
 * @author wangsong
 * @date 2022/11/17 0017 13:09
 * @return java.lang.String
 * @version 1.0.0
 */
function keywordRed(str, keyword, isCase) {
    if (str == null || str == "") {
        return str;
    }
    if (isCase) {
        keyword = keyword.toLowerCase();
    }
    let keywordLength = keyword.length;
    let sb = "";
    for (let i = 0; i < str.length; i++) {
        let val = null;
        if (i + keywordLength <= str.length) {
            val = str.substring(i, i + keywordLength);
        } else {
            val = str.substring(i);
        }
        let toVal = isCase ? val.toLowerCase() : val;
        if (toVal == keyword) {
            let pStr = "<span style=\"color: red\">" + val + "</span>";
            sb += pStr;
            i += keywordLength - 1;
        } else {
            sb += str.substring(i, i + 1);
        }
    }
    return sb;
}


/**
 * 按键 事件
 * @author wangsong
 * @mail  1720696548@qq.com
 * @date  2023/1/13 0013 16:26
 * @version 1.0.0
 */
$(document).ready(function () {

    $("body").keydown(function (event) {

        const keyCode = event.keyCode || event.which || event.charCode;
        // 有些浏览器除了通过keyCode获取输入键code，
        // 还可以通过which，charCode获取
        // ，这么写是出于浏览器兼容性考虑
        const keyCombination = event.ctrlKey;
        console.log(event.keyCode);
        if (keyCombination && keyCode == 75) {
            console.log("按下了Ctrl + k键");
        }

        /**
         * 快速定位到输入框
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        const altKeyCombination = event.altKey;
        if (altKeyCombination && keyCode == 75) {
            console.log("按下了Alt + k键");
            // 进入搜索框
            $("#doc-search").focus();
        }

        /**
         * 快速定位到输入框
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        if (altKeyCombination && keyCode == 76) {
            console.log("按下了Alt + l键");
            // 进入搜索框
            $('.xj-docs-next-tree').toggle(100);
            $('#nextTreeA').toggle();
            $('#nextTreeB').toggle();
        }

        /**
         * 搜索结果向上移动
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        if (event.keyCode == 27) {
            $(".search-box-results").hide();
        }


        /**
         * 选中搜索结果后快速进入
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        if (event.keyCode == 13) {
            console.log("----------Enter");
            let resultsDivA = $(".search-box-results-div a");
            if (resultsDivA.length > 0) {
                let href = resultsDivA.attr("href");
                window.location.href = href;
            }
        }
        /**
         * 搜索结果向上移动
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        if (event.keyCode == 38) {
            searchResultMove(1);
        }
        /**
         * 搜索结果向下移动
         * @author wangsong
         * @mail  1720696548@qq.com
         * @date  2023/1/14 0014 10:40
         * @version 1.0.0
         */
        if (event.keyCode == 40) {
            console.log("----------down");
            searchResultMove(2);
        }
    });
});


/**
 * 搜索结果 键盘上下键移动
 * @author wangsong
 * @author type 1 向上 2-向下
 * @mail  1720696548@qq.com
 * @date  2023/1/14 0014 10:42
 * @version 1.0.0
 */
function searchResultMove(type) {
    let resultsDiv = $(".search-box-results-div");
    // 选中下一个 div
    if (resultsDiv.length > 0) {
        // 获取当前选中
        let resultIndexStr = resultsDiv.attr("id");
        let resultIndex = resultIndexStr.replace("index-", "");
        let newResultIndex = type == 2 ? parseInt(resultIndex) + 1 : parseInt(resultIndex) - 1;
        // 选中下一个(移动)
        let newResultsDiv = $("#index-" + newResultIndex);
        if (newResultsDiv.length > 0) {
            $("#index-" + resultIndex).attr("class", "");
            $("#index-" + newResultIndex).attr("class", "search-box-results-div");
            if (type == 1) {
                // 滚动条上移
                let searchResultsCount = $("#searchResultsCount").html();
                if (searchResultsCount - newResultIndex > 9) {
                    $(".search-box-results").scrollTop(newResultIndex * 30);
                }
            } else {
                // 滚动条下移
                if (newResultIndex > 9) {
                    $(".search-box-results").scrollTop((newResultIndex - 9) * 30);
                }
            }
        }
    }
}
